//
//  ViewController.m
//  YCDownloadSessionManager
//
//  Created by 蓓蕾 on 2021/12/28.
//

#import "ViewController.h"
#import "DownloadViewController.h"
#import "VideoListInfoController.h"
#import "YCDownloadManager.h"
#import "YCDownloadSession.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) UISwitch *cellarSwitch;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"后台视频下载";
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArr = @[@"单个文件下载",
                 @"多视频下载",
                 @"是否允许4G下载",
                 @"磁盘剩余空间",
                 @"当前缓存大小",
                 @"清空所有视频缓存"];
    
    [self initTableView];
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
//    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
    }
    if (indexPath.row < _dataArr.count) {
        NSString *textString = _dataArr[indexPath.row];
        cell.textLabel.text = textString;
    }
    switch (indexPath.row) {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            cell.accessoryView = self.cellarSwitch;
        }
            break;
        case 3:
        {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [YCDownloadUtils fileSizeStringFromBytes:[YCDownloadUtils fileSystemFreeSize]]];
        }
            break;
        case 4:
        {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [YCDownloadUtils fileSizeStringFromBytes:[YCDownloadManager videoCacheSize]]];
        }
            break;
        case 5:
        {
            
        }
            break;
            
        default:
            break;
    }
    
    
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        DownloadViewController *vc = [[DownloadViewController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
        return;
    }else if (indexPath.row == 1){
        VideoListInfoController *vc = [[VideoListInfoController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }else if (indexPath.row==5){
        
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"是否清空所有下载文件缓存？" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //注意清空缓存的逻辑,YCDownloadSession单独下载的文件单独清理，这里的大小由YCDownloadManager控制
            [YCDownloadManager removeAllCache];
            [self.tableView reloadData];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVc addAction:confirm];
        [alertVc addAction:cancel];
        [self presentViewController:alertVc animated:true completion:nil];
    }
}

- (void)cellarSwitch:(UISwitch *)sender {
    
    [YCDownloadManager allowsCellularAccess:sender.isOn];
}

- (UISwitch *)cellarSwitch {
    
    if (!_cellarSwitch) {
        _cellarSwitch = [[UISwitch alloc] init];
        [_cellarSwitch setOn:[YCDownloadManager isAllowsCellularAccess]];
        [_cellarSwitch addTarget:self action:@selector(cellarSwitch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cellarSwitch;
}


@end
